home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-06 | 2.7 KB | 62 lines | [TEXT/GEOL] |
- Item forwarded by A33 to A34
-
- Item 3046572 2-Nov-89 18:20
-
- From: ROSENSTEIN1 Rosenstein, Larry
-
- To: D1282 Power Up,PRT
-
- cc: MACAPP.TECH$ MacApp Technical
-
- Sub: re Casting
-
- I would make 2 minor comments about your message on object casting.
-
- (1) You are right that casting an object primarily affects the compiler's view
- of the object. This is 100% true in a non-debug version of the program. When
- debugging is turned on, then an object cast generates a run time check to
- ensure that the cast is valid.
-
-
- (2) If I were implementing a TShapeList class, I wouldn't make it a subclass of
- TSortedList. I would use a TSortedList as a field in the object.
-
- In TShapeList, I would then define the exact interface that I wanted my clients
- to use. That interface could deal with TShape objects, which means that
- clients wouldn't have to do any casting. All the casts would be inside the
- implementation of TShapeList.
-
- (Another advantage is that the same class could handle different sorting
- functions, simply by initializing TShapeList with different instances of
- TSortedList.)
-
- Most of the operations defined on TShapeList would call similar methods of
- TSortedList. This adds a bit of run-time overhead, but it isn't very
- significant. (I've actually implemented a TShapeList class using this
- technique, although I used TList instead of TSortedList. In C++, you could
- make the method of TShapeList inline, and there wouldn't be any overhead at
- all.)
-
- The reason I suggest this implementation is that TShapeList isn't really a kind
- of TSortedList. TShapeList is a brand new type, unrelated to TSortedList. You
- do want to reuse the code from TSortedList, however, which is why you use an
- instance of TSortedList as a field of TShapeList.
-
- In other languages, you can do this in a more straightforward way. For
- example, in C++ you could make TSortedList a private base class of TShapeList.
- TShapeList inherits the code of TSortedList, but doesn't make the TSortedList
- interfaces public. Then you can publicize the operations of TSortedList that
- you want TShapeList to have. This doesn't solve the casting problems, however.
-
- To solve the casting problems, you would need a feature called parameterized
- types, which C++ will someday support. With this feature, one could define a
- type TSortedList<T>, where T is a type. To use this class, you need to supply
- a value for T.
-
- TShapeList would then be defined as TSortedList<TShape>. The resulting class
- is completely type-safe (all the methods deal with TShape objects), but uses
- the same code as TSortedList.
-
- Larry Rosenstein
-
-